package com.lanou3g.musicenjoy.volley;

import android.content.Context;
import android.graphics.Bitmap;

import android.support.v4.util.LruCache;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.lanou3g.musicenjoy.fragment.NewSongListFragment;


/**
 * Created by dllo on 15/10/17.
 */
public class VolleySingleton {
    public static VolleySingleton volleySingleton;
    private Context mContext;
    private ImageLoader imageLoader;
    private RequestQueue requestQueue;

    //私有化,防止其他类调用
    private VolleySingleton(Context mContext) {
        this.mContext = mContext;
        getRequestQueue();
        getImage();
    }

    //图片的二级缓存
    private void getImage() {
        imageLoader = new ImageLoader(getRequestQueue(), new ImageLoader.ImageCache() {
            LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(10*1024*1024);
            @Override
            public Bitmap getBitmap(String s) {
                return cache.get(s);
            }

            @Override
            public void putBitmap(String s, Bitmap bitmap) {
                cache.put(s, bitmap);
            }
        });
    }
    //返回imageloader
    public ImageLoader getImageLoader() {
        return imageLoader;
    }
    //返回volleysingleton的对象
    public static VolleySingleton getInstance(Context context) {
        if (volleySingleton == null) {
            synchronized (VolleySingleton.class) {
                if (volleySingleton == null) {
                    volleySingleton = new VolleySingleton(context);
                }

            }
        }
        return volleySingleton;
    }

    //初始化请求队列
    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(mContext);
        }
        return requestQueue;
    }
    //添加请求队列
    public <T> RequestQueue addQueue (Request<T> request) {
        getRequestQueue().add(request);
        return requestQueue;
    }


}
